package com.yiqi.zhiyuan.feature.order.presenter;

import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.OrderContract;
import com.yiqi.zhiyuan.feature.order.dependencies.order.DaggerOrderPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class OrderPresenter implements OrderContract.Presenter {

    private OrderContract.View mView;

    @Inject
    OrderContract.Model mModel;


    public OrderPresenter(OrderContract.View mView) {
        this.mView = mView;
        DaggerOrderPresenterComponent.create().inject(this);
    }

    @Override
    public void doSelectOrderList(int type, int pageIndex) {
        mModel.doSelectOrderList(type, pageIndex)
                .compose(mView.<PageData<OrderVO>>bindToLife())
                .subscribe(new Observer<PageData<OrderVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(PageData<OrderVO> orderVOS) {
                        mView.dismissLoading();
                        if (orderVOS .getList()!= null && orderVOS.getList().size() > 0) {
                            mView.showOrderListSuccess(orderVOS.getList());
                        } else {
                            mView.showOrderListEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderListError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void doSelectOrderListMore(int type, int pageIndex) {
        mModel.doSelectOrderList(type, pageIndex)
                .compose(mView.<PageData<OrderVO>>bindToLife())
                .subscribe(new Observer<PageData<OrderVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(PageData<OrderVO> orderVOS) {
                        mView.showOrderListMoreSuccess(orderVOS.getList());

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderListMoreError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void start() {

    }
}
